package g14.sc12project.milestone1.Weather;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import g14.sc12project.milestone1.BookStore.BookInfo;
import g14.sc12project.milestone1.Common.Utils;
import g14.sc12project.milestone1.Common.XMLParser;

public class Weather implements WeatherWS {

	XMLParser parser = null;
	Document doc = null;
	
	public Weather(String XMLDBFileName){
		parser = new XMLParser();
		doc = parser.loadXMLDoc(XMLDBFileName);
	}
	
	@Override
	public WeatherInfo queryWeahter(Date date, String location) {
		
		int year = date.getYear();
		int month  = date.getMonth();
		int day  = date.getDate();
		
		//System.out.println(year);System.out.println(month);System.out.println(day);
		
		//System.out.println(location);
		
		String selectQuery = "weathers/weather[location='"+location+"' and date[year='"+year+"'] and date[month='"+month+"'] and date[date='"+day+"']]";
		NodeList nodes = parser.getNodesByQuery(doc, selectQuery);
		//System.out.println(nodes.getLength());
		WeatherInfo weather = null;
		for (int count = 0; count < nodes.getLength(); count++) {
			Node node = nodes.item(count);
			//Node weatherNode = node.getParentNode();
			//System.out.println(nodes.item(count));
			
			Utils util=new Utils();
			weather = util.getWeatherInfo(node);
			//book= util.getBookInfo(bookNode);
			//break;
		}
		
		return weather;
	}

}
